package com.ly.web.controller.king;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kingdee.bos.webapi.entity.IdentifyInfo;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.KingConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.BaseRemoteBo;
import com.ly.heart.domain.KingBaseContactsEntity;
import com.ly.heart.domain.KingOrgEntity;
import com.ly.heart.service.IKingBaseContactsService;
import com.ly.heart.service.IKingOrgService;
import com.ly.heart.service.ISupplierService;
import com.ly.heart.util.CurrentUserUtil;
import com.ly.heart.util.KingCommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Api (value = "ContactsRemote", tags = "基础数据-联系人")
@RestController
@RequestMapping ("/heart/ContactsRemote")
@Slf4j
public class ContactsRemote {

    @Resource
    private IKingBaseContactsService baseContactsService;
    @Resource
    private IKingOrgService iKingOrgService;

    @ApiOperation ("拉取联系人")
    @PostMapping("/pull")
    public AjaxResult pullContacts () {
        KingOrgEntity orgEntity = iKingOrgService.getOne ( new LambdaQueryWrapper<KingOrgEntity> ().eq ( KingOrgEntity :: getOrgId, CurrentUserUtil.getCompanyId () ).eq ( KingOrgEntity :: getIsDisabled, false ) );
        if ( orgEntity == null ) {
            throw new CustomException ( "请先至系统管理中配置金蝶账套！" );
        }
        IdentifyInfo identifyInfo = KingCommonUtil.getIdentifyInfo ( orgEntity );
        K3CloudApi client = new K3CloudApi ( identifyInfo );
        BaseRemoteBo baseRemoteBo = new BaseRemoteBo();
        baseRemoteBo.setFormId(KingConstUtil.BD_CommonContact);
        List<String> FieldKeys = new ArrayList<>();
        FieldKeys.add("FNumber");
        FieldKeys.add("FName");
        FieldKeys.add("Fex.FNumber");
        FieldKeys.add("FPost");
        FieldKeys.add("FCompanyType");
        FieldKeys.add("FCompany.FNumber");
        FieldKeys.add("FTel");
        FieldKeys.add("FMobile");
        FieldKeys.add("FFax");
        FieldKeys.add("FEmail");
        FieldKeys.add("FBizAddress");
        FieldKeys.add("FBizLocation");
        FieldKeys.add("FBizLocNumber");
        String result = String.join(",", FieldKeys);
        baseRemoteBo.setFieldKeys(result);
        String jsonData = JSON.toJSONString(baseRemoteBo);
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery ( jsonData );
            for ( int i = 0; i < lists.size (); i++ ) {
                List<Object> objects = lists.get ( i );
                String code = (String) objects.get ( 0 );
                KingBaseContactsEntity hasContacts = baseContactsService.getOne(new LambdaQueryWrapper<KingBaseContactsEntity>().eq(KingBaseContactsEntity::getContactsCode, code));
                if(Objects.isNull(hasContacts)){
                    KingBaseContactsEntity baseContactsEntity = new KingBaseContactsEntity();
                    baseContactsEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
                    //编码
                    baseContactsEntity.setContactsCode(code);
                    //名称
                    String name = (String) objects.get ( 1 );
                    baseContactsEntity.setName(name);
                    //性别
                    if(StringUtils.isNotEmpty((String) objects.get(2))){
                        String sex = (String) objects.get(2);
                        if(sex.equals("SEX01_SYS")){
                            baseContactsEntity.setSex("0");
                        }
                        if (sex.equals("SEX02_SYS")) {
                            baseContactsEntity.setSex("1");
                        }
                    }
                    //职务
                    String position = (String) objects.get ( 3 );
                    baseContactsEntity.setPosition(position);
                    //类型
                    String type = (String) objects.get ( 4 );
                    if(type.equals(KingConstUtil.BD_Supplier)){
                        baseContactsEntity.setType("0");
                    }else{
                        baseContactsEntity.setType("1");
                    }
                    //所属公司
                    String belongCompany = (String) objects.get ( 5 );
                    baseContactsEntity.setBelongCompany(belongCompany);
                    //固定电话
                    String workTel = (String) objects.get ( 6 );
                    baseContactsEntity.setWorkTel(workTel);
                    //移动电话
                    String mobile = (String) objects.get ( 7 );
                    baseContactsEntity.setMobile(mobile);
                    //传真
                    String fax = (String) objects.get ( 8 );
                    baseContactsEntity.setFax(fax);
                    //邮箱
                    String email = (String) objects.get ( 9 );
                    baseContactsEntity.setEmail(email);
                    //详细地址
                    String address = (String) objects.get ( 10 );
                    baseContactsEntity.setAddress(address);
                    //地点名称
                    String addressName = (String) objects.get ( 11 );
                    baseContactsEntity.setAddressName(addressName);
                    //地点编码
                    String addressCode = (String) objects.get ( 12 );
                    baseContactsEntity.setAddressCode(addressCode);
                    baseContactsEntity.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
                    baseContactsEntity.setCreateTime ( DateUtils.getNowDate () );
                    baseContactsEntity.setIsSend ( KingConstUtil.KING_DATA_IS_SEND_YES );
                    baseContactsService.save(baseContactsEntity);
                }
            }
        } catch ( Exception e ) {
            throw new RuntimeException ( "同步联系人失败！" );
        }
        return AjaxResult.success ( "同步联系人成功！" );
    }





}
